
#include <iostream>
#include <map>
#include <vector>

using namespace std;

std::vector<int> solution(int n, int max, const std::vector<int> &array) {
  map<int, int> freq;
  for (int card : array) {
    freq[card]++;
  }

  vector<int> order = {1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
  vector<int> a_candidates;
  vector<int> b_canditates;

  for (int card : order) {
    if (freq[card] >= 3) {
      a_candidates.push_back(card);
    }
    if (freq[card] >= 2) {
      b_canditates.push_back(card);
    }
  }

  for (int a : a_candidates) {
    for (int b : b_canditates) {
      if (b != a && (3 * a + 2 * b) <= max) {
        return {a, b};
      }
    }
  }

  return {0, 0};
}

int main() {
  // Add your test cases here

  std::vector<int> result1 = solution(9, 34, {6, 6, 6, 8, 8, 8, 5, 5, 1});
  std::cout << (result1 == std::vector<int>{8, 5}) << std::endl;

  std::vector<int> result2 = solution(9, 37, {9, 9, 9, 9, 6, 6, 6, 6, 13});
  std::cout << (result2 == std::vector<int>{6, 9}) << std::endl;

  std::vector<int> result3 = solution(9, 40, {1, 11, 13, 12, 7, 8, 11, 5, 6});
  std::cout << (result3 == std::vector<int>{0, 0}) << std::endl;

  return 0;
}